#include <stdio.h>
#include "array.h"
#include <malloc.h>
#include <string.h>

char *pa = "123456";

void funArry(int a[])
{
    int i = sizeof(a);
    char b = a[3];
}

void funArry2(int *a)
{
    int i = sizeof(a);
    char b = a[3];
}

void funArrayChange(int a[], int lenght)
{

    int b[lenght];
    for (int i = 0; i < lenght; i++)
    {
        b[i] = a[lenght - i - 1];
    }

    for (int i = 0; i < lenght; i++)
    {
        a[i] = b[i];
    }
}

int main(int argc, char const *argv[])
{
    int a[5] = {1, 2, 3, 4, 5};
    int testC = 456;
    printf("a sizeof = %d \n", sizeof(a));
    printf("&a sizeof = %d \n", sizeof(&a));
    printf("a[5] sizeof = %d \n", sizeof(a[5]));
    printf("数组越界示例：a[5] size = %p , &testC = %p \n", &a[5], &testC);

    int *testFirst = a;

    printf("testFirst value = %d , size = %ld \n", *testFirst, sizeof(testFirst));

    *testFirst = 3;

    printf("a value = %d , size = %ld \n", a[0], sizeof(testFirst));

    char *pTestArry = "abcdef";

    printf("pTestArry pointer for four value = %d , size = %ld \n", *(pTestArry + 4), sizeof(pTestArry));
    printf("pTestArry array   for four value = %d , size = %ld \n", pTestArry[4], sizeof(pTestArry));

    char testArray[] = "abcdef";

    printf("testArray pointer for four value = %d , size = %ld \n", *(testArray + 4), sizeof(testArray));
    printf("testArray array   for four value = %d , size = %ld \n", testArray[4], sizeof(testArray));

    int array[5] = {1, 2, 3, 4, 5};
    // int *ptr=(int *)(&a+1);
    int *ptr = (int *)(&a + 1);
    printf("&a value = %d , array+1 = %d , ptr-1 = %d\n", *ptr, *(array + 1), *(ptr - 2));

    printf("\n");
    printf("数组指针大小\n");
    char *pTestArray = testArray;
    int pSize = strlen(pTestArray) / sizeof(pTestArray[0]);
    printf("pTestArray size = %d , 指向的数据大小 = %d\n\n", sizeof(pTestArray), pSize);

    struct TestStruct *pT;
    printf("TestStruct size = %ld\n", sizeof(pT));
    printf("pT & = %p , pT + 0x1 = %p\n", pT, pT + 0x1);
    printf("pT & = %d , (unsigned long)p + 0x1 = %p\n", pT, (unsigned long)pT + 0x1);
    printf("pT & = %p , (unsigned int)p + 0x1 = %p\n", pT, (unsigned int)pT + 0x1);
    printf("pT & = %p , (unsigned int)p + 0x1 = %d\n", pT, (unsigned int)pT + 0x1);

    int a2[4] = {1, 2, 3, 4};
    int *ptr1 = (int *)(&a2 + 1);
    int *ptr2 = (int *)((int)a2 + 1);
    printf("ptr1[-1] = %x , \n", ptr1[-1]);
    printf("*ptr2 = %p\n", ptr2);

    printf("\n多维数据初始化\n");
    int multipleArray[2][3] = {{1, 2, 3}, {4, 5, 6}};
    for (int i = 0; i < 2; i++)
    {
        printf("\n第 %d 行 : \n", i);
        for (int j = 0; j < 3; j++)
        {
            printf("index%d = %d ", j, multipleArray[i][j]);
        }
    }
    printf("\n\n4.5.1.3，&p[4][2] - &a[4][2]的值为多少？--------------------\n");
    printf("int a[5][5];\nint (*p)[4];\np = a;\n问&p[4][2] - &a[4][2]的值为多少？\n");
    printf("\nexample:\n");

    int a4[5][5];
    int(*p4)[4];
    p4 = a4;
    printf("&a4[4][2] = %#p , &p4[4][2] = %#p\n", &a4[4][2], &p4[4][2]);
    printf("&p4[4][2] - &a4[4][2] = %p,&p4[4][2] - &a4[4][2] = %d\n", &p4[4][2] - &a4[4][2], &p4[4][2] - &a4[4][2]);

    printf("pa value = %c \n", *pa);

    printf("数组传递示例\n");
    funArry(a4);
    funArry2(a4);
    int arrayChange[] = {1, 2, 3, 4, 5};
    printf("原始数据\n");
    for (int i = 0; i < 5; i++)
    {
        printf("index = %d , value = %d \n", i, arrayChange[i]);
    }
    funArrayChange(arrayChange, 5);
    printf("函数转换\n");
    for (int i = 0; i < 5; i++)
    {
        printf("index = %d , value = %d \n", i, arrayChange[i]);
    }
}
